﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using ScrumTable.Common;
using ScrumTable.Common.Collections;
using ScrumTable.DL.Data.Common;

#endregion

namespace ScrumTable.BL.DM.DataManagement.Tracking.EditCommands
{
    /// <summary>
    /// Contains all required information to perform undo / redo actions.
    /// </summary>
    internal class DomainCollectionUndoMoveCommand<T> : IUndoEditCommand
        where T : DomainBase, new()
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly ValueChain<Pair<string, Type>> _oldIdChain;
        private readonly ValueChain<Pair<string, Type>> _newIdChain;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------
 

        /// <summary>
        /// Gets the state change cause.
        /// </summary>
        public string Cause { get { return null; } }

        /// <summary>
        /// Gets the change type which means the element or action that
        /// caused the undo edit entry.
        /// </summary>
        /// <value></value>
        public UndoEditType Type { get { return UndoEditType.Move; } }

        /// <summary>
        /// Gets/sets the associated domain context.
        /// </summary>
        public IDomainContext Context { get; set; }


        /// <summary>
        /// Gets the name of the domain object which accommodates the change. 
        /// </summary>
        public string ElementName { get; private set; }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new DomainCollectionUndoMoveCommand instance.
        /// </summary>
        internal DomainCollectionUndoMoveCommand(
            string name,
            ValueChain<Pair<string, Type>> oldIdChain,
            ValueChain<Pair<string, Type>> newIdChain)
        {
            PreCondition.AssertNotNull(oldIdChain, "oldIdChain");
            PreCondition.AssertNotNull(newIdChain, "newIdChain");

            _oldIdChain = oldIdChain;
            _newIdChain = newIdChain;

            ElementName = name ?? string.Empty;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Executes the underlying undo command.
        /// </summary>
        void IUndoEditCommand.CallUndo()
        {
            PerformCommand(_newIdChain, _oldIdChain);
        }

        /// <summary>
        /// Executes the underlying redo command.
        /// </summary>
        void IUndoEditCommand.CallRedo()
        {
            PerformCommand(_oldIdChain, _newIdChain);
        }

        private void PerformCommand(ValueChain<Pair<string, Type>> newChain, ValueChain<Pair<string, Type>> oldChain)
        {
            // retrieve moved domain object
            T currentDomainObject = Context.TryGetObjectByChain<T>(newChain);

            if (currentDomainObject != null)
            {
                // retrieve new target collection
                DomainCollection<T> collection = Context.TryGetCollectionByChain<T>(oldChain.ParentChain);

                if (collection != null)
                    collection.AddInternal(currentDomainObject);
            }
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}